'''
通过selenium登录获取cookies
爬虫爬取所有的input和select
'''
import requests
import re
from bs4 import BeautifulSoup

# 记得修改这个
cokies = {'JSESSIONID': '47E8CF3DB6FE13547641408815F525D4'}

url1 = r'http://192.168.0.150:8082/ciis_dp/Petition_add.action#'


def get_list_input(get_url, header=None, cookies=None):
    req = requests.get(url=get_url, headers=header, cookies=cookies)
    rule = re.compile(r'\<input.+?\>')
    id_rule = re.compile(r'id=\"(.+?)\"')
    type_rule = re.compile(r'type=\"(.+?)\"')
    name_rule = re.compile(r'name=\"(.+?)\"')
    readonly_rule = re.compile(r'readonly=\"(.+?)\"')
    checked_rule = re.compile(r'checked=\"(.+?)\"')
    res = rule.findall(req.text)
    the_res = []
    for i in res:
        the_list = []
        try:
            the_list.append(id_rule.findall(i)[0])
        except:
            the_list.append(' ')
        try:
            the_list.append(type_rule.findall(i)[0])
        except:
            the_list.append(' ')
        try:
            the_list.append(name_rule.findall(i)[0])
        except:
            the_list.append(' ')
        try:
            the_list.append(readonly_rule.findall(i)[0])
        except:
            the_list.append(' ')
        try:
            the_list.append(checked_rule.findall(i)[0])
        except:
            the_list.append(' ')
        the_res.append(the_list)
    return the_res


def new_get_list_input(get_url, header=None, cookies=None):
    req = requests.get(url=get_url, headers=header, cookies=cookies)
    bs = BeautifulSoup(req.text, 'html.parser')
    l = []
    for i in bs.find_all('input'):
        l1 = []
        try:
            l1.append(i.attrs['id'])
        except:
            l1.append(' ')
        try:
            l1.append(i.attrs['type'])
        except:
            l1.append(' ')
        try:
            l1.append(i.attrs['name'])
        except:
            l1.append(' ')
        try:
            l1.append(i.attrs['readonly'])
        except:
            l1.append(' ')
        try:
            l1.append(i.attrs['checked'])
        except:
            l1.append(' ')
        l.append(l1)
    return l


def get_list_select(get_url, header=None, cookies=None):
    req = requests.get(url=get_url, headers=header, cookies=cookies)
    rule = re.compile(r'\<select.+?\>')
    id_rule = re.compile(r'id=\"(.+?)\"')
    name_rule = re.compile(r'name=\"(.+?)\"')
    res = rule.findall(req.text)
    the_res = []
    for i in res:
        the_list = []
        try:
            the_list.append(id_rule.findall(i)[0])
        except:
            the_list.append(' ')
        try:
            the_list.append(name_rule.findall(i)[0])
        except:
            the_list.append(' ')
        the_res.append(the_list)
    return the_res


def get_list_text_area(get_url, header=None, cookies=None):
    req = requests.get(url=get_url, headers=header, cookies=cookies)
    rule = re.compile(r'\<textarea.+?\>')
    id_rule = re.compile(r'id=\"(.+?)\"')
    name_rule = re.compile(r'name=\"(.+?)\"')
    class_rule = re.compile(r'class=\"(.+?)\"')
    res = rule.findall(req.text)
    the_res = []
    for i in res:
        the_list = []
        try:
            the_list.append(id_rule.findall(i)[0])
        except:
            the_list.append(' ')
        try:
            the_list.append(name_rule.findall(i)[0])
        except:
            the_list.append(' ')
        try:
            the_list.append(class_rule.findall(i)[0])
        except:
            the_list.append(' ')
        the_res.append(the_list)
    return the_res


def res1():
    L = []
    gli = new_get_list_input(get_url=url1, header=None, cookies=cokies)
    gls = get_list_select(get_url=url1, header=None, cookies=cokies)
    glta = get_list_text_area(get_url=url1, header=None, cookies=cokies)
    for i in gli:
        if i[1] == 'text':
            L.append(i)
        elif i[1] == 'radio':
            L.append(i)
    for i in gls:
        L.append(i)
    for i in glta:
        L.append(i)
    return L
